<?php

class MsgCateAction extends Action {

   function del($id,$table){

	     $cate_id=$table->field('cate_id')->where('parent_id='.$id)->select();

	     dump($cate_id);

	   /*  $d_sql =  "delete from". $GLOBALS['ecs']->table("store_cate") ."where  up_id='$id' ";
                     $GLOBALS['db']->query($d_sql);
	       while($row =  $GLOBALS['db']->fetch_array($query)){

                 echo  $cate_id = $row['cate_id'];
                  echo $del_store = 'delete from '.$GLOBALS['ecs']->table('store_items').' where cate_id ="'.$cate_id.'"';
                   $GLOBALS['db']->query($del_store);

                     del($cate_id);
	       	   }*/


	}

	function index(){
			$user= D('MsgCate');
			$data= $user->field('cate_id')->where('bpath like "0%"')->select();
            foreach ($data as $key=>$value){

            	 $a[]=$value[cate_id];
            }


			//$this->display();
			//del(1,$user);

		}


    function Msg_cate() {

		   $list         =   D('MsgCate');
           $cate         =   $list->list_cate();
           $list_cate=  $cate->field('cate_id,cate_name,parent_id,bpath,concat(bpath,"-",cate_id) as path')->order('path')->select();

            foreach ($list_cate as $key=>$val){
            	$list_cate[$key]['count']=count(explode('-',$val['bpath']));
            }
           $this->assign('list_cate',$list_cate);
           $this->display();
    }

    function insert_cate(){

    	 if(!empty($_POST['sub'])){
    	 	  if($_POST['cate_sel']!='0'){
    	 	  	$arr_path             =   explode('|',$_POST['cate_sel']);

                $_POST['bpath']      =   $arr_path[1].'-'.$arr_path[0];
    	 	  }else{

    	 	  	 $_POST['bpath'] = '0';
    	 	  }

                $_POST['parent_id']  =   $arr_path[0];

    	 	    $form                  =   D('MsgCate');

    	 	 if ($vo = $form->create()) {

                  $list = $form->add();
            header('Location:Msg_cate');
    	 }
      }
    }

   function del_cate(){


        if(!empty($_POST['sub'])){

        	$arr_path             =   explode('|',$_POST['cate_sel']);
        	$_POST['cate_id']    =   $arr_path[0];
        	$_POST['bpath']      =   $arr_path[1].'-'.$arr_path[0];
        	$form                  =   D('MsgCate');

            $form->relation(true)->delete($_POST['cate_id']);
            $data= $form->field('cate_id')->where('bpath like "'.$_POST['bpath'].'%"')->select();

            foreach ($data as $value){
           	 	$form->relation(true)->delete($value[cate_id]);
            }


            header('Location:Msg_cate');

        }
    }

   function upd_cate(){

      if(!empty($_POST['sub'])){
          $form               =   D('MsgCate');
        if ($vo = $form->create()) {
        	$arr_path           =   explode('|',$_POST['cate_sel']);
            $_POST['cate_id']  =   $arr_path['0'];
            $list               =   $form->save($_POST);

        header('Location:Msg_cate');
        }
      }
   }


}
?>